package org.snusmumr.android.media.data;

import org.snusmumr.android.media.util.Utils;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;


public class ContactAPISdk5 extends ContactAPI {
	private static final IContactMetaData META_DATA = new MetaDataSdk5();
	
	public Intent getContactIntent() {
		return(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI));
	}
	
	@Override
	public Cursor getContactsCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		if(Utils.isEmpty(selection))
			selection = META_DATA.displayName() + " is not null";
		else
			selection += " and " + META_DATA.displayName() + " is not null";
		Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                getBasicContactProjection(), selection, selectionArgs, sortOrder);
		
		return cur;
	}
	
	@Override
	public IContactMetaData getMetaData() {
		return META_DATA;
	}
	
	public long insertContact(String name) {
		ContentValues values = new ContentValues();
		values.put(Data.DISPLAY_NAME, name);
		Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
		long rawContactId = ContentUris.parseId(rawContactUri);
		long contactId = getContactId(rawContactId);
		System.out.println("rawContactId = " + rawContactId);
		System.out.println("contactId = " + contactId);

		values.clear();
		values.put(Data.MIMETYPE, Data.CONTENT_TYPE);
		values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
		values.put(Data.RAW_CONTACT_ID, rawContactId);
		getContentResolver().insert(Data.CONTENT_URI, values);

		values.clear();
		values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
		values.put(Data.RAW_CONTACT_ID, rawContactId);
		getContentResolver().insert(Data.CONTENT_URI, values);
		
		//TODO test issue
		return contactId;
	}
	
	private long getContactId(long rawContactId) {
	    Cursor cur = null;
	    try {
	        cur = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, 
	        		new String[] { ContactsContract.RawContacts.CONTACT_ID }, 
	        		ContactsContract.RawContacts._ID + "=" + rawContactId, null, null);
	        if (cur.moveToFirst()) {
	            return cur.getLong(cur.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        if (cur != null) {
	            cur.close();
	        }
	    }
	    return -1l;
	}
	
	/**
	 * Android 2.x implementation of IContactMetaData
	 * @author Denis Kruglyakov
	 *
	 */
	private final static class MetaDataSdk5 implements IContactMetaData {

		@Override
		public String id() {
			return ContactsContract.Contacts._ID;
		}

		@Override
		public String displayName() {
			return ContactsContract.Contacts.DISPLAY_NAME;
		}

		public Uri getUri() {
			return ContactsContract.Contacts.CONTENT_URI;
		}
	}
}
